<?php
class MembersController extends Controller
{
	public $layout='//layouts/main';

	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view', 'cec', 'selc','profile'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

  public function actionIndex()
  {
    $user = Users::model()->find('id = :id', array(':id' => $_SESSION['id']));
    $photo = Photos::model()->with("user")->find('profile_picture = :profile_picture and user_id = :user_id', array(":profile_picture" => 1, "user_id" => $_SESSION['id']));

    $member_id = $_SESSION['member_id'];
    $pageSize = 10;

    //News------------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    $news = News::model()->findAll($criteria);

    //Articles-------------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    $articles = Articles::model()->findAll($criteria);

    //Links-------------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    $links = Links::model()->findAll($criteria);

    //Journals-----------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    //Pagination
    $journals_pages = new CPagination(Journals::model() -> count());
    $journals_pages -> pageSize = $pageSize;
    $journals_pages -> applyLimit($criteria);

    $journals = Journals::model()->findAll($criteria);

    //Webinars-----------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    //Pagination
    $webinars_pages = new CPagination(Webinars::model() -> count());
    $webinars_pages -> pageSize = $pageSize;
    $webinars_pages -> applyLimit($criteria);

    $webinars = Webinars::model()->findAll($criteria);

    //Download URLs-----------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    //Pagination
    $downloadUrls_pages = new CPagination(DownloadUrls::model() -> count());
    $downloadUrls_pages -> pageSize = $pageSize;
    $downloadUrls_pages -> applyLimit($criteria);

    $downloadUrls = downloadUrls::model()->findAll($criteria);

		$this->render('index',array(
			'photo'=>$photo,
      'user'=>$user,
      'news' => $news,
      'articles' => $articles,
      'links' => $links,
      'journals' => $journals,
      'journals_pages' => $journals_pages,
      'webinars' => $webinars,
      'webinars_pages' => $webinars_pages,
      'downloadUrls' => $downloadUrls,
      'downloadUrls_pages' => $downloadUrls_pages,
		));
  }

	public function actionCec()
  {
    $this->render('cec');
  }

  public function actionSelc()
  {
    $this->render('selc');
  }

  public function actionView($id)
  {
    $user = Users::model()->findByPk($id);

    $this->render('view', array(
      'user' => $user,
    ));
  }

	public function loadModel($id)
	{
		$model=Members::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='members-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
